package ui.panel.showroom;

import cerebrum.common.CommonConst;
import cerebrum.common.OsUtil;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.StageStyle;
import ui.factory.SingletonFactory;
import ui.util.Window;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;

import static uk.co.caprica.vlcj.javafx.videosurface.ImageViewVideoSurfaceFactory.videoSurfaceForImageView;

public class VideoWallpaperStage extends Window {
    private static VideoWallpaperStage instance = null;
    private final MediaPlayerFactory mediaPlayerFactory;

    private EmbeddedMediaPlayer embeddedMediaPlayer;

    private ImageView videoImageView;

    //调用单例工厂
    public static VideoWallpaperStage getInstance() {
        if (instance == null) {
            instance = SingletonFactory.getWeakInstace(VideoWallpaperStage.class);
        }
        return instance;
    }

    public VideoWallpaperStage() {

        this.mediaPlayerFactory = new MediaPlayerFactory();
        this.embeddedMediaPlayer = mediaPlayerFactory.mediaPlayers().newEmbeddedMediaPlayer();
        this.embeddedMediaPlayer.events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
            @Override
            public void playing(MediaPlayer mediaPlayer) {
            }

            @Override
            public void paused(MediaPlayer mediaPlayer) {
            }

            @Override
            public void stopped(MediaPlayer mediaPlayer) {
                System.out.println("stop");
            }

            @Override
            public void timeChanged(MediaPlayer mediaPlayer, long newTime) {
            }
        });
        this.videoImageView = new ImageView();
        this.videoImageView.setPreserveRatio(true);
        embeddedMediaPlayer.videoSurface().set(videoSurfaceForImageView(this.videoImageView));
        BorderPane root = new BorderPane();
        root.setStyle("-fx-background-color: black;");

        videoImageView.fitWidthProperty().bind(root.widthProperty());
        videoImageView.fitHeightProperty().bind(root.heightProperty());

        root.widthProperty().addListener((observableValue, oldValue, newValue) -> {
            // If you need to know about resizes
        });

        root.heightProperty().addListener((observableValue, oldValue, newValue) -> {
            // If you need to know about resizes
        });

        root.setCenter(videoImageView);

        Scene scene = new Scene(root, CommonConst.SYSTEMWIDTH, CommonConst.SYSTEMHEIGHT, Color.BLACK);
        scene.setFill(Paint.valueOf("#ffffff00"));
        setTitle("IceoLogy_VideoWallpaper");
        setScene(scene);
        initStyle(StageStyle.TRANSPARENT);


//        setMedia("D:\\desserts\\2.mp4");

        embeddedMediaPlayer.controls().setPosition(0.4f);


    }


    public final void stop() {
        embeddedMediaPlayer.controls().stop();
        embeddedMediaPlayer.release();
        mediaPlayerFactory.release();
    }


    public void setMedia(String path) {
        //关闭计时器
        embeddedMediaPlayer.media().play(path
                , ":no-audio"
                , ":rtsp=tcp"
                , ":codec=ffmpeg"
                , ":vout=any"
                , ":avcodec-threads=1"
                , ":avcodec-hw=any"
                , ":network-caching=200"
                , ":prefetch-buffer-size=1024"
                , ":prefetch-read-size=65535"
                , ":file-caching=1000"
        );
        //循环播放
        embeddedMediaPlayer.controls().setRepeat(true);
    }


    public void showStage() {

        setMedia("D:\\desserts\\mp4\\2.mp4");
        show();
        OsUtil.setWinIconAfter("IceoLogy_VideoWallpaper");
    }

    /**
     * 播放
     */
    public void play() {
        embeddedMediaPlayer.controls().play();
    }

    /**
     * 暂停
     */
    public void pausePlayer() {
        //关闭当前播放的媒体
        embeddedMediaPlayer.controls().pause();
    }

    public void close() {
        VideoWallpaperStage.getInstance().close();
        //关闭当前播放的媒体
        embeddedMediaPlayer.controls().stop();
    }
}